package com.mingtu.guards.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.StringUtils;
import com.mingtu.guards.R;
import com.mingtu.guards.bean.TaskTemBean;

import java.util.ArrayList;
import java.util.List;

public class Task2Adapter extends RecyclerView.Adapter<Task2Adapter.ActivityHolder> implements View.OnClickListener {
    private Context context;
    private List<TaskTemBean.PageBean.ListBean> mData = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener = null;


    public Task2Adapter(Context context, List<TaskTemBean.PageBean.ListBean> mData) {
        this.context = context;
        this.mData = mData;
    }

    @Override
    public ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_task, parent, false);
        // 实例化viewholder
        ActivityHolder viewHolder = new ActivityHolder(view);
        viewHolder.itemView.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ActivityHolder holder, int position) {

        if (mData != null) {
            if (mData.size() > 0) {
                TaskTemBean.PageBean.ListBean listBean = mData.get(position);
                String taskName = listBean.getName();
                String beginTime = listBean.getCreateTime();
                String endTime = listBean.getFinishTime();
                int status = listBean.getStatus();
                String isFinish = listBean.getIsFinish();


                if (!StringUtils.isEmpty(taskName)) {
                    holder.tvTaskName.setText(taskName + "");
                }

                if (!StringUtils.isEmpty(beginTime)) {
                    holder.tvCreateTime.setText("开始时间：" + beginTime);
                }

                if (!StringUtils.isEmpty(endTime)) {
                    holder.tvEndTime.setText("截止时间：" + endTime);
                }

                holder.tvTaskType.setText("任务类型：其他任务");

                if (isFinish.equals("0")) {
                    holder.layoutStatus.setVisibility(View.VISIBLE);
                    holder.tvCompleteRate.setVisibility(View.GONE);
                    holder.tvTaskName.setTextColor(context.getResources().getColor(R.color.text_color333));
                    setTextColor(holder, context.getResources().getColor(R.color.text_color666));
                } else if (isFinish.equals("1")) {
                    holder.layoutStatus.setVisibility(View.GONE);
                    holder.tvCompleteRate.setVisibility(View.VISIBLE);
                    holder.tvCompleteRate.setText("已完成");
                    holder.tvCompleteRate.setTextColor(context.getResources().getColor(R.color.text_color999));
                    holder.tvTaskName.setTextColor(context.getResources().getColor(R.color.text_color999));
                    setTextColor(holder, context.getResources().getColor(R.color.text_color999));
                }
            }

        }
        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }


    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    /**
     * 设置字体颜色
     *
     * @param color
     */
    private void setTextColor(ActivityHolder holder, int color) {
        holder.tvTaskType.setTextColor(color);
        holder.tvCreateTime.setTextColor(color);
        holder.tvEndTime.setTextColor(color);
    }

    public void upData(List<TaskTemBean.PageBean.ListBean> data) {
        mData = data;
        notifyDataSetChanged();
    }

    public void clearData() {
        mData.clear();
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public static class ActivityHolder extends RecyclerView.ViewHolder {
        private final View view;
        private final TextView tvTaskName;
        private final LinearLayout layoutStatus;
        private final TextView tvStatus;
        private final TextView tvTaskType;
        private final TextView tvCreateTime;
        private final TextView tvEndTime;
        private final TextView tvComplete;
        private final TextView tvCompleteRate;


        public ActivityHolder(View itemView) {
            super(itemView);
            view = itemView;
            tvTaskName = (TextView) view.findViewById(R.id.tv_task_name);
            layoutStatus = (LinearLayout) view.findViewById(R.id.layout_status);
            tvStatus = (TextView) view.findViewById(R.id.tv_status);
            tvTaskType = (TextView) view.findViewById(R.id.tv_task_type);
            tvCreateTime = (TextView) view.findViewById(R.id.tv_create_time);
            tvEndTime = (TextView) view.findViewById(R.id.tv_end_time);
            tvComplete = (TextView) view.findViewById(R.id.tv_complete);
            tvCompleteRate = (TextView) view.findViewById(R.id.tv_complete_rate);
        }
    }

}
